<template>
  <div class="msgStepTwo-container">
    <div class="content-part">
      <doctorMsg ref="doctorMsg"></doctorMsg>
      <!-- <div class="tips">*按要求填写你的基本信息和病情，方便我们知道您是谁</div> -->
      <formMsg @getpatientInfo="saveformInfos"></formMsg>
    </div>
    <div class="bottomtips">*患者信息仅{{docName}}医生可见</div>
    <div class="btn-box">
      <div class="weui-btn weui-btn_primary" :class="{'btn-disabled':!ischecked}" @click="gotoSession" >确认，开始对话医生</div>
    </div>
  </div>
</template>

<script>
import doctorMsg from "@/views/components/doctorInfo/doctorInfo";
import formMsg from "@/views/components/formMsg/formMsg";
import { putPatientInfo } from "@/api/dialog";
export default {
  created() {
  },
  mounted() {
    this.docName = this.$refs.doctorMsg.doctorInfo.name

  },
  data() {
    return {
      ischecked: false,
      docName: "",
      // 这个值可能在点击按钮的时候传给服务器
      pinfo: {},
      // doctorInfo: {}
    }
  },
  computed: {
    // doctorInfo () {
    //   doctorInfo = this.$store.state.doctorInfo || {}
    //   return doctorInfo
    // },
    openid () {
      let openid = this.$route.query.openid || {}
      return openid
    }
  },
  methods: {
    saveformInfos(params) {
      // 获得患者选中的来源值
      this.pinfo = params.patientInfo
      // this.pinfo.openid = "ovvFdwxU1NOAtIu3-PPCXklCLc_Q";
      this.ischecked = params.requireAll
      // 将从formMsg获取的数据保存到sessionStorage里面
      window.sessionStorage.setItem("infos", this.pinfo)
    },
    // 开始与医生对话
    gotoSession() {
      if (this.ischecked) {
        // 清空session
        window.sessionStorage.removeItem("infos")
        // 将第二步的信息传人store组合到完整的用户信息
        this.$store.commit("updatepatientInfo", this.pinfo)
        // 将store里面患者完整信息上传到服务器
        // 如果提交错误，跳转404页面
          let sessionId = window.sessionStorage.getItem('sessionId')
          let doctorid = this.$route.query.doctorid
          let openid = this.$route.query.openid
        // 发送第一条消息
        // 如果对话列表是空，就创建第一条消息
        let patientInfo = this.$store.state.patientInfo
        let to = sessionId.replace('team-', '')
					this.$store.dispatch('sendMsg', {
							type: 'custom',
							scene: 'team',
							to: to,
							content: {
								type: 4,
								data: patientInfo
							}
					})
        window.sessionStorage.setItem('patientSourceInfo',JSON.stringify(this.patientInfo))
        putPatientInfo(this.$store.state.patientInfo).then((err)=>{
          if (!err) {
            }
        })
        if(sessionId) {
          this.$router.push({path: `/patientChat/${sessionId}`,query:{doctorid:doctorid,openid:openid}})
        }else{
          this.$router.push({path:'/dialog/pataintSession',query:{openid:openid}})
        }
      }
    }
  },
  components: {
    doctorMsg,
    formMsg
  }
};
</script>

<style scoped lang="stylus">
.msgStepTwo-container
  width: 100%
  background: #f1f1f1
  padding: 0 10px 10px 10px
  box-sizing : border-box
  .content-part
    padding: 0 10px
    padding-bottom: 10px
    background: #fff
    border-radius : 4px
  .bottomtips
    margin-top: 14px
    margin-left: 14px
    font-size: 13px
  .btn-box
    width: 100%;
    box-sizing: border-box
    margin-top: 14px

    .weui-btn
      border: none
      border-radius: 20px
      font-size: 1.1875rem !important
      &.weui-btn_primary
        border: none
        background: #108ee9
      &.btn-disabled
        border: none
        background: #c3e3f9
</style>
